{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import Series, DataFrame"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Series的排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "s1 = Series(np.random.randn(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0.353695\n",
       "1    0.516444\n",
       "2   -1.267883\n",
       "3    0.491114\n",
       "4    0.044430\n",
       "5    0.420707\n",
       "6    0.015520\n",
       "7    0.157224\n",
       "8   -0.507988\n",
       "9    0.816873\n",
       "dtype: float64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.35369474,  0.51644394, -1.26788296,  0.49111431,  0.04443035,\n",
       "        0.42070653,  0.01552016,  0.15722365, -0.50798797,  0.81687252])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RangeIndex(start=0, stop=10, step=1)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "s2 = s1.sort_values(ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9    0.816873\n",
       "1    0.516444\n",
       "3    0.491114\n",
       "5    0.420707\n",
       "0    0.353695\n",
       "7    0.157224\n",
       "4    0.044430\n",
       "6    0.015520\n",
       "8   -0.507988\n",
       "2   -1.267883\n",
       "dtype: float64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0.353695\n",
       "1    0.516444\n",
       "2   -1.267883\n",
       "3    0.491114\n",
       "4    0.044430\n",
       "5    0.420707\n",
       "6    0.015520\n",
       "7    0.157224\n",
       "8   -0.507988\n",
       "9    0.816873\n",
       "dtype: float64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2.sort_index()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Dataframe的排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df1 = DataFrame(np.random.randn(40).reshape(8,5), columns=['A','B','C','D','E'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-1.407659</td>\n",
       "      <td>0.434508</td>\n",
       "      <td>-0.769402</td>\n",
       "      <td>-1.557372</td>\n",
       "      <td>-0.439336</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-2.369773</td>\n",
       "      <td>0.335388</td>\n",
       "      <td>0.955628</td>\n",
       "      <td>-0.377313</td>\n",
       "      <td>-1.523516</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.499351</td>\n",
       "      <td>3.306794</td>\n",
       "      <td>-0.280044</td>\n",
       "      <td>-0.054490</td>\n",
       "      <td>-0.061546</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.150991</td>\n",
       "      <td>0.178401</td>\n",
       "      <td>0.470797</td>\n",
       "      <td>0.452184</td>\n",
       "      <td>-0.103362</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2.538143</td>\n",
       "      <td>-0.672921</td>\n",
       "      <td>0.553710</td>\n",
       "      <td>-0.650690</td>\n",
       "      <td>-0.507762</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>-2.182388</td>\n",
       "      <td>0.526380</td>\n",
       "      <td>2.196212</td>\n",
       "      <td>-0.646108</td>\n",
       "      <td>-1.315029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.081426</td>\n",
       "      <td>-0.827773</td>\n",
       "      <td>1.728363</td>\n",
       "      <td>-0.127423</td>\n",
       "      <td>-0.834192</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>-0.665540</td>\n",
       "      <td>0.081015</td>\n",
       "      <td>0.111719</td>\n",
       "      <td>0.162778</td>\n",
       "      <td>0.850304</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          A         B         C         D         E\n",
       "0 -1.407659  0.434508 -0.769402 -1.557372 -0.439336\n",
       "1 -2.369773  0.335388  0.955628 -0.377313 -1.523516\n",
       "2 -0.499351  3.306794 -0.280044 -0.054490 -0.061546\n",
       "3 -0.150991  0.178401  0.470797  0.452184 -0.103362\n",
       "4  2.538143 -0.672921  0.553710 -0.650690 -0.507762\n",
       "5 -2.182388  0.526380  2.196212 -0.646108 -1.315029\n",
       "6  0.081426 -0.827773  1.728363 -0.127423 -0.834192\n",
       "7 -0.665540  0.081015  0.111719  0.162778  0.850304"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1   -2.369773\n",
       "5   -2.182388\n",
       "0   -1.407659\n",
       "7   -0.665540\n",
       "2   -0.499351\n",
       "3   -0.150991\n",
       "6    0.081426\n",
       "4    2.538143\n",
       "Name: A, dtype: float64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1['A'].sort_values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "df2 = df1.sort_values('A')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-2.369773</td>\n",
       "      <td>0.335388</td>\n",
       "      <td>0.955628</td>\n",
       "      <td>-0.377313</td>\n",
       "      <td>-1.523516</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>-2.182388</td>\n",
       "      <td>0.526380</td>\n",
       "      <td>2.196212</td>\n",
       "      <td>-0.646108</td>\n",
       "      <td>-1.315029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-1.407659</td>\n",
       "      <td>0.434508</td>\n",
       "      <td>-0.769402</td>\n",
       "      <td>-1.557372</td>\n",
       "      <td>-0.439336</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>-0.665540</td>\n",
       "      <td>0.081015</td>\n",
       "      <td>0.111719</td>\n",
       "      <td>0.162778</td>\n",
       "      <td>0.850304</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.499351</td>\n",
       "      <td>3.306794</td>\n",
       "      <td>-0.280044</td>\n",
       "      <td>-0.054490</td>\n",
       "      <td>-0.061546</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.150991</td>\n",
       "      <td>0.178401</td>\n",
       "      <td>0.470797</td>\n",
       "      <td>0.452184</td>\n",
       "      <td>-0.103362</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.081426</td>\n",
       "      <td>-0.827773</td>\n",
       "      <td>1.728363</td>\n",
       "      <td>-0.127423</td>\n",
       "      <td>-0.834192</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2.538143</td>\n",
       "      <td>-0.672921</td>\n",
       "      <td>0.553710</td>\n",
       "      <td>-0.650690</td>\n",
       "      <td>-0.507762</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          A         B         C         D         E\n",
       "1 -2.369773  0.335388  0.955628 -0.377313 -1.523516\n",
       "5 -2.182388  0.526380  2.196212 -0.646108 -1.315029\n",
       "0 -1.407659  0.434508 -0.769402 -1.557372 -0.439336\n",
       "7 -0.665540  0.081015  0.111719  0.162778  0.850304\n",
       "2 -0.499351  3.306794 -0.280044 -0.054490 -0.061546\n",
       "3 -0.150991  0.178401  0.470797  0.452184 -0.103362\n",
       "6  0.081426 -0.827773  1.728363 -0.127423 -0.834192\n",
       "4  2.538143 -0.672921  0.553710 -0.650690 -0.507762"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-1.407659</td>\n",
       "      <td>0.434508</td>\n",
       "      <td>-0.769402</td>\n",
       "      <td>-1.557372</td>\n",
       "      <td>-0.439336</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-2.369773</td>\n",
       "      <td>0.335388</td>\n",
       "      <td>0.955628</td>\n",
       "      <td>-0.377313</td>\n",
       "      <td>-1.523516</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.499351</td>\n",
       "      <td>3.306794</td>\n",
       "      <td>-0.280044</td>\n",
       "      <td>-0.054490</td>\n",
       "      <td>-0.061546</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.150991</td>\n",
       "      <td>0.178401</td>\n",
       "      <td>0.470797</td>\n",
       "      <td>0.452184</td>\n",
       "      <td>-0.103362</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2.538143</td>\n",
       "      <td>-0.672921</td>\n",
       "      <td>0.553710</td>\n",
       "      <td>-0.650690</td>\n",
       "      <td>-0.507762</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>-2.182388</td>\n",
       "      <td>0.526380</td>\n",
       "      <td>2.196212</td>\n",
       "      <td>-0.646108</td>\n",
       "      <td>-1.315029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.081426</td>\n",
       "      <td>-0.827773</td>\n",
       "      <td>1.728363</td>\n",
       "      <td>-0.127423</td>\n",
       "      <td>-0.834192</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>-0.665540</td>\n",
       "      <td>0.081015</td>\n",
       "      <td>0.111719</td>\n",
       "      <td>0.162778</td>\n",
       "      <td>0.850304</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          A         B         C         D         E\n",
       "0 -1.407659  0.434508 -0.769402 -1.557372 -0.439336\n",
       "1 -2.369773  0.335388  0.955628 -0.377313 -1.523516\n",
       "2 -0.499351  3.306794 -0.280044 -0.054490 -0.061546\n",
       "3 -0.150991  0.178401  0.470797  0.452184 -0.103362\n",
       "4  2.538143 -0.672921  0.553710 -0.650690 -0.507762\n",
       "5 -2.182388  0.526380  2.196212 -0.646108 -1.315029\n",
       "6  0.081426 -0.827773  1.728363 -0.127423 -0.834192\n",
       "7 -0.665540  0.081015  0.111719  0.162778  0.850304"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.sort_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-1.407659</td>\n",
       "      <td>0.434508</td>\n",
       "      <td>-0.769402</td>\n",
       "      <td>-1.557372</td>\n",
       "      <td>-0.439336</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-2.369773</td>\n",
       "      <td>0.335388</td>\n",
       "      <td>0.955628</td>\n",
       "      <td>-0.377313</td>\n",
       "      <td>-1.523516</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.499351</td>\n",
       "      <td>3.306794</td>\n",
       "      <td>-0.280044</td>\n",
       "      <td>-0.054490</td>\n",
       "      <td>-0.061546</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.150991</td>\n",
       "      <td>0.178401</td>\n",
       "      <td>0.470797</td>\n",
       "      <td>0.452184</td>\n",
       "      <td>-0.103362</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2.538143</td>\n",
       "      <td>-0.672921</td>\n",
       "      <td>0.553710</td>\n",
       "      <td>-0.650690</td>\n",
       "      <td>-0.507762</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>-2.182388</td>\n",
       "      <td>0.526380</td>\n",
       "      <td>2.196212</td>\n",
       "      <td>-0.646108</td>\n",
       "      <td>-1.315029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.081426</td>\n",
       "      <td>-0.827773</td>\n",
       "      <td>1.728363</td>\n",
       "      <td>-0.127423</td>\n",
       "      <td>-0.834192</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>-0.665540</td>\n",
       "      <td>0.081015</td>\n",
       "      <td>0.111719</td>\n",
       "      <td>0.162778</td>\n",
       "      <td>0.850304</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          A         B         C         D         E\n",
       "0 -1.407659  0.434508 -0.769402 -1.557372 -0.439336\n",
       "1 -2.369773  0.335388  0.955628 -0.377313 -1.523516\n",
       "2 -0.499351  3.306794 -0.280044 -0.054490 -0.061546\n",
       "3 -0.150991  0.178401  0.470797  0.452184 -0.103362\n",
       "4  2.538143 -0.672921  0.553710 -0.650690 -0.507762\n",
       "5 -2.182388  0.526380  2.196212 -0.646108 -1.315029\n",
       "6  0.081426 -0.827773  1.728363 -0.127423 -0.834192\n",
       "7 -0.665540  0.081015  0.111719  0.162778  0.850304"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Series和DataFrame的排序.ipynb     imdb.csv\r\n",
      "Untitled.ipynb\r\n"
     ]
    }
   ],
   "source": [
    "!ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_results = pd.read_csv('imdb.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df_resu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
